package com.kfm.company.config;

import com.kfm.company.interceptor.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class MyConfig implements WebMvcConfigurer {

    @Autowired
    private MyInterceptor myInterceptor;

    @Autowired
    private LoginCountInterceptor loginCountInterceptor;
    @Autowired
    private AccessIPInterceptor accessIPInterceptor;

    @Autowired
    private RequestManyInterceptor requestManyInterceptor;

    @Autowired
    private TestInterceptor testInterceptor;

    private List<String> ALLOW_PATH = List.of("/login", "/register","/captcha", "/css/**","/js/**","/imgs/**", "/font/**", "/favico.ico", "/error");

    /**
     * 配置拦截器 再该方法中注册拦截器信息
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addInterceptor 配置拦截器对象
        // addPathPatterns 配置哪些进入该拦截器
        // excludePathPatterns 配置哪些请求不进入拦截器
        // registry.addInterceptor(myInterceptor).addPathPatterns("/**").
        //         excludePathPatterns("/login", "/captcha", "/js/**", "/css/**", "/font/**", "/error");
        registry.addInterceptor(loginCountInterceptor).addPathPatterns("/login");
        registry.addInterceptor(requestManyInterceptor).addPathPatterns("/**").excludePathPatterns("/error", "/captcha", "/css/**","/js/**","/imgs/**", "/font/**", "/favico.ico");
        registry.addInterceptor(accessIPInterceptor).addPathPatterns("/**").excludePathPatterns("/error");
        // 注册拦截器 相当于 @WebFilter("/*") 拦截所有请求，除了ALLOW_PATH中的请求
        registry.addInterceptor(testInterceptor).addPathPatterns("/**").excludePathPatterns(ALLOW_PATH);
    }

    /**
     * 静态资源的映射
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
        registry.addResourceHandler("/font/**").addResourceLocations("classpath:/static/font/");
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
        registry.addResourceHandler("/imgs/**").addResourceLocations("classpath:/static/imgs/");
        registry.addResourceHandler("/avatar/**").addResourceLocations("file:E:/upload/avatar/");
    }
}
